package com.tsl.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tsl.reggie.common.R;
import com.tsl.reggie.domain.po.Employee;
import com.tsl.reggie.mapper.EmployeeMapper;
import com.tsl.reggie.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 员工信息 服务实现类
 * </p>
 *
 * @author 唐书亮
 * @since 2024-01-11
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Override
    public R<Employee> checkLogin(HttpServletRequest request, Employee employee) {
        //1将密码加密
        String password=null;
        String username=null;
        password = DigestUtils.md5DigestAsHex( employee.getPassword().getBytes());

        //2.根据用户名查询信息
        LambdaQueryWrapper<Employee> lambdaQueryWrapper= new LambdaQueryWrapper();
        LambdaQueryWrapper<Employee> eq = lambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee one = getOne(eq);
        //3.如果没有查询到,则返回登陆失败
        if (one == null){
            return R.error("登录失败");
        }
        //4.与查询的返回的密码比对
        if (!password.equals(one.getPassword())){
            return R.error("密码错误");
        }
        //5检查用户的状态,如果为0则登陆失败
        if (one.getStatus() == 0 ){
            return R.error("登陆失败");
        }
        //6将用户id存入session里
        request.getSession().setAttribute("userId",one.getId());
        //6密码,状态都没有问题,登录成功,返回登录成功信息
        return R.success(one);
    }
}
